home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2330 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: wkaufman.us.oracle.com!wkaufman
  2. From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: I/O Question
  5. Date: 20 Jan 1996 16:11:08 GMT
  6. Organization: Oracle Corporation, Redwood Shores CA
  7. Message-ID: <4dr46s$sab@inet-nntp-gw-1.us.oracle.com>
  8. References: <4dp1nm$mu6@taco.cc.ncsu.edu>
  9. NNTP-Posting-Host: wkaufman.us.oracle.com
  10.  
  11. In article <4dp1nm$mu6@taco.cc.ncsu.edu> Alex David Groce <adgroce> writes:
  12. ] while (scanf("%i", &list[count++]) != EOF); // Read elements.
  13. ] count--; // Avoid good old OBOB, the Off-By-One-Bug.
  14. ] Would it be worth my time to learn buffered I/O techniques, read or
  15. ] getchar or such, to input this data?  Every little bit of time
  16. ] counts...  Or is there a good example somewhere of a fast way to do
  17. ] this?
  18.  
  19.     The <stdio.h> functions are already buffered.  There's probably not
  20. much you can do on that end to speed this up.  A couple of ideas,
  21. though:
  22.  
  23.     a) Change the program to use a file (and fscanf()) instead of stdin.
  24. On some implementations, stdin may be effectively buffered one line at a
  25. time; but files are usually buffered by about 2Kb (or whatever BUFSIZ is
  26. #define'd as in <stdio.h>).
  27.  
  28.     b) If you've got a lot of memory to throw at the problem, you could
  29. try using setbuf() or setvbuf() (esp. with a static array) to increase
  30. the buffer size (but that might actually slow down your program if it
  31. starts paging,...).
  32.  
  33.     c) Calling fgets() to get a line and strtol() to parse it may be
  34. faster than using *scanf() functions, since scanf() does a lot more than
  35. just parse numbers.
  36.  
  37.     And, the number one response to "How can I make this faster":
  38.  
  39.     d) Use a profiling tool, find out where the time is actually going,
  40. and concentrate on those parts of your program.  Most compilers come
  41. with such a tool: on Unix, check the man pages for prof(1) and gprof(1),
  42. and for your compiler to see if it's got an option like "-p" or "-pg";
  43. or, if you're horribly under-budget, I'd heartily recommend "Quantify"
  44. from Pure Software.
  45.  
  46.                                            -- Bill K.
  47.  
  48. Bill Kaufman,          | ``Can anyone tell me what happened to my
  49. wkaufman@us.oracle.com |   "Hello" and why?'' -- David Sheffield
  50.